home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 12 / Example 12.4 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  6.5 KB  |  261 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 12.4: Skybox Example                                //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include <vector>
  9. #include "debug.h"
  10. #include "skybox.h"
  11. #include "mouse.h"
  12.  
  13. class APPLICATION
  14. {
  15.     public:
  16.         APPLICATION();
  17.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  18.         HRESULT Update(float deltaTime);
  19.         HRESULT Render();
  20.         HRESULT Cleanup();
  21.         HRESULT Quit();
  22.         DWORD FtoDword(float f){return *((DWORD*)&f);}
  23.  
  24.     private:
  25.         IDirect3DDevice9* m_pDevice; 
  26.         MOUSE m_mouse;
  27.         SKYBOX *m_pSkybox;
  28.  
  29.         D3DLIGHT9 m_light;
  30.         HWND m_mainWindow;
  31.         INTPOINT m_lastM;
  32.         D3DXVECTOR3 m_rot;
  33. };
  34.  
  35. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  36. {
  37.     APPLICATION app;
  38.  
  39.     if(FAILED(app.Init(hInstance, 800, 600, true)))return 0;
  40.  
  41.     MSG msg;
  42.     memset(&msg, 0, sizeof(MSG));
  43.     int startTime = timeGetTime(); 
  44.  
  45.     while(msg.message != WM_QUIT)
  46.     {
  47.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  48.         {
  49.             ::TranslateMessage(&msg);
  50.             ::DispatchMessage(&msg);
  51.         }
  52.         else
  53.         {    
  54.             int t = timeGetTime();
  55.             float deltaTime = (t - startTime)*0.001f;
  56.  
  57.             app.Update(deltaTime);
  58.             app.Render();
  59.  
  60.             startTime = t;
  61.         }
  62.     }
  63.  
  64.     app.Cleanup();
  65.  
  66.     return msg.wParam;
  67. }
  68.  
  69. APPLICATION::APPLICATION()
  70. {
  71.     m_pDevice = NULL; 
  72.     m_mainWindow = 0;
  73.     srand(GetTickCount());
  74.     m_pSkybox = NULL;
  75.     m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  76. }
  77.  
  78. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  79. {
  80.     debug.Print("Application initiated");
  81.  
  82.     //Create Window Class
  83.     WNDCLASS wc;
  84.     memset(&wc, 0, sizeof(WNDCLASS));
  85.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  86.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  87.     wc.hInstance     = hInstance;
  88.     wc.lpszClassName = "D3DWND";
  89.  
  90.     //Register Class and Create new Window
  91.     RegisterClass(&wc);
  92.     m_mainWindow = CreateWindow("D3DWND", "Example 12.4: Skybox Example", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  93.     SetCursor(NULL);
  94.     ShowWindow(m_mainWindow, SW_SHOW);
  95.     UpdateWindow(m_mainWindow);
  96.  
  97.     //Create IDirect3D9 Interface
  98.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  99.  
  100.     if(d3d9 == NULL)
  101.     {
  102.         debug.Print("Direct3DCreate9() - FAILED");
  103.         return E_FAIL;
  104.     }
  105.  
  106.     //Check that the Device supports what we need from it
  107.     D3DCAPS9 caps;
  108.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  109.  
  110.     //Hardware Vertex Processing or not?
  111.     int vp = 0;
  112.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  113.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  114.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  115.  
  116.     //Check vertex & pixelshader versions
  117.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  118.     {
  119.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  120.     }
  121.  
  122.     //Set D3DPRESENT_PARAMETERS
  123.     D3DPRESENT_PARAMETERS d3dpp;
  124.     d3dpp.BackBufferWidth            = width;
  125.     d3dpp.BackBufferHeight           = height;
  126.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  127.     d3dpp.BackBufferCount            = 1;
  128.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  129.     d3dpp.MultiSampleQuality         = 0;
  130.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  131.     d3dpp.hDeviceWindow              = m_mainWindow;
  132.     d3dpp.Windowed                   = windowed;
  133.     d3dpp.EnableAutoDepthStencil     = true; 
  134.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  135.     d3dpp.Flags                      = 0;
  136.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  137.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  138.  
  139.     //Create the IDirect3DDevice9
  140.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  141.                                  vp, &d3dpp, &m_pDevice)))
  142.     {
  143.         debug.Print("Failed to create IDirect3DDevice9");
  144.         return E_FAIL;
  145.     }
  146.  
  147.     //Release IDirect3D9 interface
  148.     d3d9->Release();
  149.  
  150.     // Create m_light
  151.     ::ZeroMemory(&m_light, sizeof(m_light));
  152.     m_light.Type      = D3DLIGHT_DIRECTIONAL;
  153.     m_light.Ambient   = D3DXCOLOR(0.5, 0.5, 0.5, 1.0f);
  154.     m_light.Diffuse   = D3DXCOLOR(0.9, 0.9, 0.9, 1.0f);
  155.     m_light.Specular  = D3DXCOLOR(0.5, 0.5, 0.5, 1.0f);
  156.     D3DXVECTOR3 dir = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
  157.     D3DXVec3Normalize(&dir, &dir);
  158.     m_light.Direction = dir;    
  159.     m_pDevice->SetLight(0, &m_light);
  160.     m_pDevice->LightEnable(0, true);
  161.  
  162.     m_mouse.InitMouse(m_pDevice, m_mainWindow);
  163.     m_lastM = m_mouse;
  164.  
  165.     m_pSkybox = new SKYBOX(m_pDevice, "textures/skybox", 300.0f);
  166.  
  167.     //Set sampler state
  168.     for(int i=0;i<4;i++)
  169.     {
  170.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  171.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  172.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  173.     }
  174.  
  175.     return S_OK;
  176. }
  177.  
  178. HRESULT APPLICATION::Update(float deltaTime)
  179. {
  180.     m_mouse.Update();
  181.  
  182.     //Update Camera
  183.     if(m_mouse != m_lastM)
  184.     {
  185.         m_rot.y -= (m_lastM.x - m_mouse.x) * 0.01f;
  186.         m_rot.z += (m_lastM.y - m_mouse.y) * 0.01f;
  187.         m_lastM = m_mouse;
  188.     }
  189.  
  190.     //Keayboard input
  191.     if(KEYDOWN(VK_SPACE))
  192.     {
  193.     }
  194.     if(KEYDOWN(VK_RETURN))
  195.     {
  196.     }
  197.     else if(KEYDOWN(VK_ESCAPE))
  198.     {
  199.         Quit();
  200.     }
  201.  
  202.     return S_OK;
  203. }    
  204.  
  205. HRESULT APPLICATION::Render()
  206. {
  207.     // Clear the viewport
  208.     m_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
  209.  
  210.     //Set camera
  211.     D3DXMATRIX r;
  212.     D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  213.     D3DXVECTOR3 focus = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
  214.     D3DXVec3TransformNormal(&focus, &focus, &r);
  215.     D3DXVec3Normalize(&focus, &focus);
  216.  
  217.     D3DXMATRIX view, proj, world, identity;
  218.     D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &focus, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  219.     D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.4f, 1.33333f, 0.01f, 1000.0f);
  220.     D3DXMatrixIdentity(&identity);
  221.  
  222.     m_pDevice->SetTransform(D3DTS_WORLD, &identity);
  223.     m_pDevice->SetTransform(D3DTS_VIEW, &view);
  224.     m_pDevice->SetTransform(D3DTS_PROJECTION, &proj);
  225.  
  226.     // Begin the scene 
  227.     if(SUCCEEDED(m_pDevice->BeginScene()))
  228.     {
  229.         m_pSkybox->Render(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
  230.  
  231.         m_mouse.Paint();
  232.  
  233.         // End the scene.
  234.         m_pDevice->EndScene();
  235.         m_pDevice->Present(0, 0, 0, 0);
  236.     }
  237.  
  238.     return S_OK;
  239. }
  240.  
  241. HRESULT APPLICATION::Cleanup()
  242. {
  243.     try
  244.     {
  245.         if(m_pSkybox)delete m_pSkybox;
  246.  
  247.         m_pDevice->Release();
  248.  
  249.         debug.Print("Application terminated");
  250.     }
  251.     catch(...){}
  252.  
  253.     return S_OK;
  254. }
  255.  
  256. HRESULT APPLICATION::Quit()
  257. {
  258.     ::DestroyWindow(m_mainWindow);
  259.     ::PostQuitMessage(0);
  260.     return S_OK;
  261. }